<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shopcart
{

    public $shopobj;
    public $shopname = "shopcart";
    public $shopsession;
    
    function Shopcart()
    {
    	$CI =& get_instance();//访问ci核心类
    	$this->shopobj = $CI->session;
        $session_id = $this->shopobj->userdata($this->shopname);
        if($session_id)
        {
        	$this->shopsession = $session_id;
        }else
        {
        	$this->shopobj->set_userdata($this->shopname,"");
        	$this->shopsession = $this->shopobj->userdata($this->shopname);
        }
    }
	
	
	

	function add($foodid,$num)
	{
		
        if(empty($this->shopsession[$foodid]))
		{
			$data = $this->shopsession;
			$data[$foodid] = $num;
			$this->shopobj->set_userdata($this->shopname,$data);
		}else
		{
			$data = $this->shopsession;
			$data[$foodid] = $data[$foodid]+$num;
			$this->shopobj->set_userdata($this->shopname,$data);
		}


	}
	function del($foodid,$num)
	{
		if(!empty($this->shopsession[$foodid]))
		{
			if($num==0)
			{
					$data = $this->shopsession;
					unset($data[$foodid]);
					$this->shopobj->set_userdata($this->shopname,$data);
			}else
			{
				if($this->shopsession[$foodid]>$num)
				{
					$data = $this->shopsession;
					$data[$foodid] = $data[$foodid]-$num;
					$this->shopobj->set_userdata($this->shopname,$data);
				}else
				{
					$data = $this->shopsession;
					unset($data[$foodid]);
					$this->shopobj->set_userdata($this->shopname,$data);
				}
			}
			
		}
	}
	
	function clear()
	{
		$this->shopobj->set_userdata($this->shopname,"");
	}


}



?>